<template>
  <div class="header">
    <router-link class="header__abs" tag="div" to="/" v-show="showIcon">
      <div class="iconfont header__abs__back">&#xe658;</div>
    </router-link>
    <div class="header__fixed" v-show="!showIcon" :style="opacityStyle">
      <router-link to="/">
        <div class="iconfont header__fixed__back">&#xe658;</div>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: "DetailHeader",
  data() {
    return {
      showIcon: true,
      opacityStyle: {
        opacity: 0,
      },
    };
  },
  methods: {
    handleScroll() {
      // 1. 获取离页面顶部的距离 top
      const top = document.documentElement.scrollTop;
      // console.log(top);
      // 2. 离顶部多少距离时，哪个显示，哪个隐藏
      // 3. 因为 top 是动态的，所以可以通过 top 来改变透明的，实现渐变
      if (top > 60) {
        let opacity = top / 140;
        opacity = opacity > 1 ? 1 : opacity;
        this.opacityStyle = { opacity };
        this.showIcon = false;
      } else {
        this.showIcon = true;
      }
    },
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  unmounted() {
    window.removeEventListener("scroll", this.handleScroll);
  },
};
</script>

<style lang="scss" scoped>
.header {
  &__abs {
    position: absolute;
    top: 0.2rem;
    left: 0.2rem;
    width: 0.8rem;
    height: 0.8rem;
    line-height: 0.8rem;
    border-radius: 0.4rem;
    text-align: center;
    background: rgba(0, 0, 0, 0.8);
    &__back {
      font-size: 0.5rem;
      color: #fff;
    }
  }
  &__fixed {
    z-index: 3;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 0.86rem;
    line-height: 0.86rem;
    color: #fff;
    background-color: #00bcd4;
    font-size: 0.3rem;
    text-align: center;
    &__back {
      position: absolute;
      top: 0;
      left: 0;
      width: 0.64rem;
      text-align: center;
      font-size: 0.5rem;
      color: #fff;
    }
  }
}
</style>